package com.ews.onlineexchange.service.schedule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.TradeStatus;
import com.ews.onlineexchange.service.BulletinService;
import com.ews.onlineexchange.service.SmsService;
import com.ews.onlineexchange.service.UnoveredAndPassedApplyBulletinList;
import com.ews.onlineexchange.service.UnoveredBulletinList;

@Named
@Singleton
@Startup
@TransactionAttribute( TransactionAttributeType.REQUIRED )
public class BulletinAutoPassTimer {

	@Resource
	TimerService						timerService;

	@Inject
	EntityManager						entityManager;

	@Inject
	BulletinService						bulletinEndService;

	@Inject
	UnoveredBulletinList				unoveredBulletinList;

	@Inject
	UnoveredAndPassedApplyBulletinList	unoveredAndPassedApplyBulletinList;

	@Inject
	SmsService							smsService;

	@PostConstruct
	public void init() {
//		List<Bulletin> resultList = getUnoveredBulletinList();	
//		for ( Bulletin bulletin : resultList ) {
//			if ( bulletin.isAutoPass() ) {
//				timerService.createTimer( bulletin.getEndingApplyTime(), bulletin );				
//			}
//		}
		
		List<Bulletin> unoveredAndPassedApplyBulletins = unoveredAndPassedApplyBulletinList.getResultList();
		for ( Bulletin bulletin : unoveredAndPassedApplyBulletins ) {
			if ( bulletin.isAutoPass() && bulletin.isNotEnoughApply() ) {
				bulletin.setTradeStatus( TradeStatus.Breaked );
				bulletin.setOver( true );
				entityManager.merge( bulletin );				
			}
		}

	}

	public List<Timer> getAllTimers() {
		return new ArrayList<>( timerService.getAllTimers() );
	}

	public String registerNewBulletin( Bulletin bulletin ) {
		Timer timer = timerService.createTimer( bulletin.getEndingApplyTime(), bulletin );
		System.out.println( "======================================================" );
		System.out.println( timer.isPersistent() );
		System.out.println( "======================================================" );
		return "registered";
	}

	/* 当手动开始时，取消已生成的timer */
	public void cancelBulletinTimer( Bulletin bulletin ) {
		Collection<Timer> timers = timerService.getAllTimers();
		for ( Timer timer : timers ) {
			if ( timer.getInfo() != null
					&& timer.getInfo().toString().startsWith( "com.ews.onlineexchange.model.AutoPassBulletin" ) ) {
				Bulletin b = (Bulletin) timer.getInfo();
				if ( b.getId().equals( bulletin.getId() ) ) {
					timer.cancel();
				}
			}

		}
	}

	@Timeout
	public void timeout( Timer timer ) {
		Bulletin bulletin = (Bulletin) timer.getInfo();
		if ( bulletin.getTradeStatus().equals( TradeStatus.Opened ) ) {
			if ( bulletin.isAutoPass() && bulletin.isNotEnoughApply() ) {
				bulletin.setTradeStatus( TradeStatus.Breaked );
				bulletin.setOver( true );
				entityManager.merge( bulletin );			
			}
		}

		// smsService.addMessageForSupplier(bulletin);
		// smsService.addMessageForBuyer(bulletin);
	}

	private List<Bulletin> getUnoveredBulletinList() {
		return unoveredBulletinList.getResultList();
	}

}
